package com.wuxiaofan.product.service;

import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.product.entity.Category;
import com.wuxiaofan.product.entity.custom.CategoryCustom;
import com.wuxiaofan.product.mapper.CategoryMapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wuxiaofan.product.client.Idgenerator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;

@Service
@Slf4j
public class CategoryService {
    @Autowired
    private CategoryMapper pmsCategoryMapper;
    @Resource
    private Idgenerator idgenerator;
    public CategoryCustom findById(String id){
        Category category = pmsCategoryMapper.selectById(id);
        CategoryCustom categoryCustom = new CategoryCustom();
        BeanUtils.copyProperties(category,categoryCustom);
        return categoryCustom;
    }

    public List<CategoryCustom> findByParentCid(Long parentCid){
        return pmsCategoryMapper.findByParentCid(parentCid);
    }

    public CategoryCustom findCountByMap(Map paraMap){
        return pmsCategoryMapper.findCountByMap(paraMap);
    }

    public CategoryCustom findGroupByMap(Map paraMap){
        return pmsCategoryMapper.findGroupByMap(paraMap);
    }

    public List<CategoryCustom> findByMap(Map paraMap){
        return pmsCategoryMapper.findByMap(paraMap);
    }

    public Page<CategoryCustom> findByMap(Map paraMap, Page page){
        return page.setRecords(pmsCategoryMapper.findByMap(paraMap,page));
    }


    public void updateMapById(Map paraMap){
        pmsCategoryMapper.updateMapById(paraMap);
    }
    public void delById(String id){
        pmsCategoryMapper.deleteById(id);
    }

    public void delByIds(List<Long> ids){
        pmsCategoryMapper.delByIds(ids);
    }



    public Category save(Category pmsCategory){
        pmsCategory.setCatId(idgenerator.getId());
        pmsCategoryMapper.insert(pmsCategory);
        return pmsCategory;
    }


    public CategoryCustom tree(){
        CategoryCustom categoryCustom = findById("0");
        List<Category> allCategory = pmsCategoryMapper.selectByMap(new HashMap<>());
        List<CategoryCustom> categoryCustoms = new ArrayList<>();
        for (Category category : allCategory) {
            if(category.getCatId().toString().equals("0")){
                continue;
            }
            CategoryCustom tmp = new CategoryCustom();
            BeanUtils.copyProperties(category,tmp);
            categoryCustoms.add(tmp);
        }
        setChildrenList(categoryCustom,categoryCustoms);
        return categoryCustom;
    }

    private void setChildrenList(CategoryCustom categoryCustom,List<CategoryCustom> allCategory){
        List<CategoryCustom> categoryList = allCategory.stream()
                .filter(tmp -> categoryCustom.getCatId().equals(tmp.getParentCid()))
                .collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(categoryList)){
            categoryList.stream().forEach(tmp -> {
                setChildrenList(tmp,allCategory);
            });
            categoryCustom.setChildrenList(categoryList);
        }
    }

}
